package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.code.dao.AlbumMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.code.entity.ImageItems;
import com.code.entity.PageResult;
import com.code.pojo.Album;
import com.code.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service(interfaceClass = AlbumService.class)
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    AlbumMapper albumMapper;

    @Override
    @Transactional
    public void delete(Integer id) {
        albumMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Album album) {
        albumMapper.updateByPrimaryKeySelective(album);
    }

    @Override
    public void add(Album album) {
        album.setImageItems("[]");
        albumMapper.insert(album);
    }

    @Override
    public PageResult<Album> findPage(Integer page,Integer size,Album album) {
        PageHelper.startPage(page,size);
        Example example = new Example(Album.class);
        Example.Criteria criteria = example.createCriteria();
        if (album.getTitle()!=null&&!album.getTitle().equals("")){
            criteria.andLike("title","%"+album.getTitle()+"%");
        }
        List<Album> list = albumMapper.selectByExample(example);
        PageInfo<Album> pageInfo = new PageInfo<>(list);
        PageResult<Album> result = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        return result;
    }

    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    @Override
    public Album findById(Integer id) {
        return albumMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateImage(Album album) {
        Album albumSql = albumMapper.selectByPrimaryKey(album.getId());
        String albumItemsOld=albumSql.getImageItems();
        String albumItemsNew=album.getImageItems();
        if (albumItemsOld.equals("[]")){
            albumItemsOld=albumItemsNew;
        }else {
            albumItemsOld=albumItemsOld.substring(0,albumItemsOld.length()-1)+","+albumItemsNew.substring(1);
        }
        albumSql.setImageItems(albumItemsOld);
        albumMapper.updateByPrimaryKeySelective(albumSql);
    }

    @Override
    public void delOne(Integer id, String uids) {
        Album album = albumMapper.selectByPrimaryKey(id);
        String imageItems = album.getImageItems();
        List<ImageItems> imageItems1 = JSONObject.parseArray(imageItems, ImageItems.class);
        for (ImageItems items : imageItems1) {
            if (items.getUid().equals(uids)){
                imageItems1.remove(items);
                break;
            }
        }
        String imageItemsNew = JSONObject.toJSONString(imageItems1);
        album.setImageItems(imageItemsNew);
        albumMapper.updateByPrimaryKeySelective(album);
    }

    @Override
    public void moveAlbum(Integer albumId, String toMoveUids, Integer movedAlbumId) {
//        老相册
        Album albumOld = albumMapper.selectByPrimaryKey(albumId);
        List<ImageItems> imageItemsOld =JSONObject.parseArray(albumOld.getImageItems(), ImageItems.class);
//        新相册
        Album albumNew = albumMapper.selectByPrimaryKey(movedAlbumId);
        List<ImageItems> imageItemsNew =JSONObject.parseArray(albumNew.getImageItems(), ImageItems.class);
//       定义变量 先找到老相册的uid然后移除
        ImageItems toMoveItem=null;
        for (ImageItems imageItems : imageItemsOld) {
            if (imageItems.getUid().equals(toMoveUids)){
                imageItemsOld.remove(imageItems);
                toMoveItem=imageItems;
                break;
            }
        }
//        放入要转移的新相册里
        imageItemsNew.add(toMoveItem);
//        更新，新老相册信息
        albumOld.setImageItems(JSONObject.toJSONString(imageItemsOld));
        albumNew.setImageItems(JSONObject.toJSONString(imageItemsNew));
        albumMapper.updateByPrimaryKeySelective(albumOld);
        albumMapper.updateByPrimaryKeySelective(albumNew);

    }
}